
<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class WeixinAction extends AdminAction {
    
    #API设置
    public function setting(){
    	$this->display();
    }
    
    #保存配置
    public function insert(){

    	$file=I('post.files');
		unset($_POST['files']);
		
		if($this->update_config($_POST,CONF_PATH.$file)){
			$state = 'SUCCESS';
          	$message = '操作成功';
		}else{
			$state = '操作失败';
		}
		echo $this->echo_json_str($state,$message,$url);
    } 

    //自定义菜单
    public function menu(){
        $list = M('WeixinMenu')->where('fid=0')->order('sort asc , id asc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['child'] = M('WeixinMenu')->where('fid='.$value['id'])->order('sort asc , id asc')->select();
        }
        $this->assign('list',$list);
    	$this->display();
    }

    public function addmenu(){
        $menu = M('WeixinMenu')->where('fid=0')->order('sort asc , id asc')->select();
        $this->assign('menu',$menu);
    	$this->display();
    }

    //保存菜单
    public function save(){
        $obj = D('WeixinMenu');
        if ($data = $obj->create()) {
            //主菜单是否超过三个
            if ($data['fid']==0) {
                $r = M('WeixinMenu')->where('fid=0')->count();                
                if ($r>=3) {
                    $state = '一级菜单不能超过3个';
                    echo $this->echo_json_str($state,$message,$url); die;
                }
            }
            //子菜单是否超过五个
            if ($data['fid']>0) {
                $r = M('WeixinMenu')->where('fid='.$data['fid'])->count();
                if ($r>=5) {
                    $state = '二级菜单不能超过5个';
                    echo $this->echo_json_str($state,$message,$url);  die;
                }            
            }
            if ($list = $obj->add($data)) {                
                $state = 'SUCCESS';
                $message = '操作成功';
                $url = U('Weixin/close');
            } else {
                $state = '操作失败';
            }  
        }else{
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    //编辑菜单
    public function editmenu(){
        $id = (int) $_GET['id'];

        if (!isset ($id)) {
            $this->error('参数错误');
        }

        $obj = M('WeixinMenu');

        $list = $obj->where("id=$id")->find();

        if (!$list) {
            $this->error('信息不存在');
        } else {
            if ($list['fid']>0) {
                $menu = M('WeixinMenu')->where('fid=0')->order('sort asc , id asc')->select();
                $this->assign('menu',$menu);
            }
            $this->assign('list', $list);
            $this->display();
        }
    }

    //保存菜单
    public function update(){
        $obj = D('WeixinMenu');
        if ($data = $obj->create()) {
            //子菜单是否超过五个
            if ($data['fid']>0) {
                $map['fid'] = $data['fid'];
                $map['id'] = array('neq',$data['id']);
                $r = M('WeixinMenu')->where($map)->count();
                if ($r>=5) {
                    $state = '二级菜单不能超过5个';
                    echo $this->echo_json_str($state,$message,$url);  die;
                }            
            }
            if ($list = $obj->save($data)) {                
                $state = 'SUCCESS';
                $message = '操作成功';
                $url = U('Weixin/close');
            } else {
                $state = '操作失败';
            }  
        }else{
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    public function delmenu(){
        $id=I('get.id');
        if($id==''){
            $this->error('您没有选择任何信息！');
        }else{
            $obj = M('WeixinMenu');
            $obj->where('fid='.$id)->delete();
            $obj->where('id='.$id)->delete();
            $this->success('操作成功',U('Weixin/menu'));
        }
    }

    //生成菜单
    public function make(){
        //删除菜单
        $access_token = $this->get_access_token();
        $del = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$access_token;
        file_get_contents($del);

        //创建菜单
        $url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;

        $list = M('WeixinMenu')->where('fid=0')->order('sort asc , id asc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['child'] = M('WeixinMenu')->where('fid='.$value['id'])->order('sort asc , id asc')->select();

        }
        $this->assign('list',$list);
        $data = $this->fetch("menutpl");

        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        $errorno = curl_errno($ch);
        if ($errorno) {
            $state = "curl未开启";
        }else{
            $js=json_decode($tmpInfo,1);
            if ($js['errcode']=='0'){
                $state = 'SUCCESS';
                $message = '操作成功';
            }else {
                $state = '错误代码：'.$js['errcode'].'，errmsg：'.$js['errmsg'];
            }
        }

        echo $this->echo_json_str($state,$message); 
    }
}